(function(){
    angular.module('app').controller("dspSkuMangeCtrl",function($scope,proxy){
        $scope.filter = {
            self: '1'
        }
        $scope.platformList = [{name:'全部平台'},{id:'android',name:'Android'},{id:'ios',name:'iOS'}]
        $scope.chargeTypeList = [{name:'全部资源类型'},{id:'cpc',name:'CPC'},{id:'cpm',name:'CPM'}]
        $scope.dealTypeList = [{name:'全部采买类型'},{id:'pdb',name:'保价保量'},{id:'pd',name:'保价不保量'}]
        $scope.stateList = [{name:'全部状态'},{id:'10',name:'待执行'},{id:'20',name:'执行中'},{id:'30',name:'交易结束'},{id:'40',name:'已售罄'}]

        $scope.chooseDate = function (start, end) {
            $scope.filter.startDate = start;
            $scope.filter.endDate = end;
            getList();
        }
        $scope.search = function(val) {
            $scope.filter.name = val;
            getList();
        }
        $scope.selectState = function(item) {
            $scope.filter.executeState = item.id;
            getList();
        }
        $scope.selectPlatform= function(item) {
            $scope.filter.platform = item.id;
            getList();
        }
        $scope.selectChargeType = function(item) {
            $scope.filter.chargeType = item.id;
            getList();
        }
        $scope.selectDealType = function(item) {
            $scope.filter.skuType = item.id;
            getList();
        }

        getList();

        function getList(current, pagesize) {
            var data = {
                'page': (current || 1)+"",
                'size': (pagesize || 20)+"",
                "sort": ["cTime,1"],
                'filter': $scope.filter,
            };
            proxy.proxy({
                url: STATIC_URL.domian+'/dsp/sku/show/list',
                data: data,
                load:true,
                success: function(res){
                    var arr = res.data.data || [];
                    var skuTypes = {
                        pdb: '保价保量',
                        pd: '保价不保量',
                        rtb: '竞价',
                    }
                    var executeStates = {
                        '10': '待执行',
                        '20': '执行中',
                        '30': '交易结束',
                        '40': '已售罄',
                    }
                    arr.forEach(function (v) {
                        v.skuType = skuTypes[v.skuType];
                        v.executeState = executeStates[v.executeState];
                        var dateStr = "",spaceStr = "";
                        v.date = v.date || [];
                        v.date.forEach(function (data) {
                            dateStr += data.startDate + ' - ' + data.endDate + ";";
                        })
                        v.spaceInfo.forEach(function (space) {
                            spaceStr += space.appName + "，" + space.adxSpaceName + ";";
                        })
                        v.date = dateStr;
                        v.spaceInfo = spaceStr;
                    })

                    // 获得列表
                    $scope.list = arr;

                    $("#pageToolbar").html("");
                    $('#pageToolbar').Paging({
                        pagesize: pagesize || '20',
                        current: current || '1',
                        count:res.data.total,
                        toolbar:true,
                        'hash': false,
                        callback:getList
                    });
                }
            })
        }

        // 下载报表
        // $scope.download = function() {
        //     var data = {
        //         'page': "1",
        //         'size': "99999",
        //         "sort": ["cTime,1"],
        //         'filter': $scope.filter,
        //     };
        //     proxy.proxy({
        //         url: STATIC_URL.domian+'/dsp/sku/show/list',
        //         data: data,
        //         success: function(res){
        //             var arr = res.data.data || [];
        //             var skuTypes = {
        //                 pdb: '保价保量',
        //                 pd: '保价不保量',
        //                 rtb: '竞价',
        //             }
        //             var executeStates = {
        //                 '10': '待执行',
        //                 '20': '执行中',
        //                 '30': '交易结束',
        //                 '40': '已售罄',
        //             }
        //             arr.forEach(function (v) {
        //                 v.skuType = skuTypes[v.skuType];
        //                 v.executeState = executeStates[v.executeState];
        //                 var dateStr = "",spaceStr = "";
        //                 v.date.forEach(function (data) {
        //                     dateStr += data.startDate + ' - ' + data.endDate + ";";
        //                 })
        //                 v.spaceInfo.forEach(function (space) {
        //                     spaceStr += space.appName + "，" + space.adxSpaceName + ";";
        //                 })
        //                 v.date = dateStr;
        //                 v.spaceInfo = spaceStr;
        //             })
        //             exportCsv({
        //                 title: ['资源名称','状态','采买时段','采买类型','单价(元/千次曝光)','量级(千次曝光/每天)','广告位信息'],
        //                 titleForKey: ['name','executeState','date','skuType','price','amount','spaceInfo'],
        //                 data: arr,
        //             },"合约资源列表.csv");
        //         }
        //     })
        // }
    })
})();